package com.bootcamp.ex1125.exception.handler;

import com.bootcamp.ex1125.bean.BaseRespVO;
import com.bootcamp.ex1125.exception.InvalidNameException;
import com.bootcamp.ex1125.exception.LoginException;
import com.bootcamp.ex1125.exception.ParamException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.NoSuchElementException;

/**
 * @program: springboot
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-25 19:44
 **/

@ControllerAdvice
public class UserExceptionHandler {

    /* 参数异常，则返回就是jsp视图 */
    @ExceptionHandler(ParamException.class)
    public ModelAndView userNameException(Exception ex){
        ModelAndView view = new ModelAndView();
        view.setViewName("/exception.jsp");
        view.addObject("exception",ex.getMessage());
        return view;
    }

    /* 未登录hello异常，username异常，则返回 json*/
    @ResponseBody
    @ExceptionHandler({InvalidNameException.class, LoginException.class})
    public BaseRespVO userException2(Exception ex){
        return BaseRespVO.error(ex.getMessage());
    }

}
